﻿using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace ProjectDanmaku.UI
{
    public class TouchPanel : MonoBehaviour, IPointerMoveHandler, IPointerDownHandler, IPointerUpHandler
    {
        private Vector2 _lastTouchPos = Vector2.zero;
        private bool _pressed = false;
        
        [SerializeField] private UnityEvent<Vector2> onTouchMove;
        
        public void OnPointerMove(PointerEventData eventData)
        {
            if (_pressed)
            {
                var delta = eventData.position - _lastTouchPos;

                onTouchMove.Invoke(delta * 0.6f);

                _lastTouchPos = eventData.position;
            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            _pressed = true;
            _lastTouchPos = eventData.position;
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            _pressed = false;
        }
    }
}